import Qt 4.7

Rectangle {
    id: search
    property alias text: searchText.text
    property alias defaultText: defaultText.text
    property variant model
    signal submit

    width: 400
    height: 40

    Rectangle {
        anchors { margins: 5; fill: parent; }
        radius: 3
        border.width: 1
        border.color: "#8D8D8D"

        Button {
            id: buttonFind
            text: "Find"
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.margins: 0
            onClicked: {
                search.submit()
            }
        }

        TextInput {
            id: searchText
            property string suggestion
            property string oldText

            function getSuggestion(prefix) {
                var tags = model.toArray()
                var suggestions = tags.filter(function(tag) {
                    tag = tag.value
                    return tag.length > prefix.length && prefix == tag.substr(0, prefix.length)
                })
                return suggestions[0] && suggestions[0].value
            }

            anchors {
                top: parent.top; right: buttonFind.left; bottom: parent.bottom; left: parent.left;
                topMargin: 2; rightMargin: 5; bottomMargin: 2; leftMargin: 5;
            }
            font.pixelSize: parent.height - anchors.topMargin - anchors.bottomMargin - Math.ceil(parent.height * 0.15)
            color: "black"
            onAccepted: {
                search.submit()
                focus = false
            }
            onTextChanged: {
                var last_index = text.lastIndexOf(' ')
                if(last_index === -1) {
                    last_index = 0
                }
                else {
                    last_index += 1
                }

                if(suggestion === undefined || text.substr(last_index) === suggestion || text.substr(last_index).length === 0) {
                    return
                }

                if(oldText === text) {
                    oldText = text.slice(0, -1)
                    return
                }
                oldText = text

                var last_index = text.lastIndexOf(' ')
                if(last_index === -1) {
                    last_index = 0
                }
                else {
                    last_index += 1
                }

                var newSuggestion = getSuggestion(text.substr(last_index))
                console.log(text.substr(last_index), newSuggestion)
                if(newSuggestion === undefined) {
                    return
                }
                suggestion = newSuggestion

                var oldCursorPosition = cursorPosition
                text = text + suggestion.substr(text.substr(last_index).length)
                moveCursorSelection(oldCursorPosition, TextEdit.SelectCharacters)
            }

            Keys.onTabPressed: {
                moveCursorSelection(text.length)
            }

            Keys.onEscapePressed: {
                focus = false
            }
        }

        Text {
            id: defaultText
            anchors.fill: parent
            anchors { topMargin: 2; rightMargin: 5; bottomMargin: 2; leftMargin: 5; }
            font.pixelSize: searchText.font.pixelSize
            opacity: {
                if(searchText.text === "") {
                    if(searchText.focus) {
                        0.4
                    }
                    else {
                        0.6
                    }
                }
                else {
                    0.0
                }
            }
        }
    }
}
